// 作为头节点链接到链表中 privatevoidlinkFirst(E e){ final Node<E> f = first; final Node<E> newNode = new Node<>(null, e, f); first = newNode; // 若f为null, 则表明链表为空 if (f == null) last = newNode; else f.prev = newNode; size++; modCount++; }
// 作为尾节点链接到链表中 voidlinkLast(E e){ final Node<E> l = last; final Node<E> newNode = new Node<>(l, e, null); last = newNode; // 若l为null, 则表明链表为空 if (l == null) first = newNode; else l.next = newNode; size++; modCount++; }
// 将新节点插在指定节点前 voidlinkBefore(E e, Node<E> succ){ // assert succ != null; final Node<E> pred = succ.prev; final Node<E> newNode = new Node<>(pred, e, succ); succ.prev = newNode; if (pred == null) first = newNode; else pred.next = newNode; size++; modCount++; }
Object[] a = c.toArray(); int numNew = a.length; if (numNew == 0) returnfalse;
Node<E> pred, succ; // 若指定位置就是尾节点时 if (index == size) { succ = null; pred = last; } else { succ = node(index); pred = succ.prev; }
for (Object o : a) { @SuppressWarnings("unchecked") E e = (E) o; Node<E> newNode = new Node<>(pred, e, null); // 确保头节点始终不为空 if (pred == null) first = newNode; else pred.next = newNode; pred = newNode; }
// 确保尾节点不为空 if (succ == null) { last = pred; } else { pred.next = succ; succ.prev = pred; }
public E remove(int index){ checkElementIndex(index); return unlink(node(index)); } public E removeFirst(){ final Node<E> f = first; if (f == null) thrownew NoSuchElementException(); return unlinkFirst(f); }
public E removeLast(){ final Node<E> l = last; if (l == null) thrownew NoSuchElementException(); return unlinkLast(l); }
// 删除非空头节点 private E unlinkFirst(Node<E> f){ // assert f == first && f != null; final E element = f.item; final Node<E> next = f.next; f.item = null; f.next = null; // 方便GC进行垃圾回收 first = next; // 若链表中只有单个节点 if (next == null) last = null; else next.prev = null; size--; modCount++; return element; }
// 删除非空尾节点 private E unlinkLast(Node<E> l){ // assert l == last && l != null; final E element = l.item; final Node<E> prev = l.prev; l.item = null; l.prev = null; // 方便GC进行垃圾回收 last = prev; // 若链表中只有单个节点 if (prev == null) first = null; else prev.next = null; size--; modCount++; return element; }
// 删除指定节点 E unlink(Node<E> x){ // assert x != null; final E element = x.item; final Node<E> next = x.next; final Node<E> prev = x.prev;
// 若prev指向的上一个节点为空,那么该节点为头节点 if (prev == null) { first = next; } else { prev.next = next; x.prev = null; }
// 若next指向的下一个节点为空,那么该节点为尾节点 if (next == null) { last = prev; } else { next.prev = prev; x.next = null; }
publicbooleanremove(Object o){ // 若给定值为null if (o == null) { // 在这里就不能缩减范围提高效率了,只能老实的进行从头遍历到尾 for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) { unlink(x); returntrue; } } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) { unlink(x); returntrue; } } } returnfalse; }
查询操作 - get()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
public E get(int index){ checkElementIndex(index); return node(index).item; }
public E getFirst(){ final Node<E> f = first; if (f == null) thrownew NoSuchElementException(); return f.item; }
public E getLast(){ final Node<E> l = last; if (l == null) thrownew NoSuchElementException(); return l.item; }
修改操作 - set()
1 2 3 4 5 6 7
public E set(int index, E element){ checkElementIndex(index); Node<E> x = node(index); E oldVal = x.item; x.item = element; return oldVal; }